import { creepBodyEnum, creepRoleEnum, creepRuleNameEnum } from "../../enum";
import { objIDToObj, roomNameToObj } from "../baseTools";
import { eventMap } from "../../modules/event/eventMap";
import taskManager from "../../modules/task/taskManager";
import eventManager from "../../modules/event/eventManager";

/**根据生产任务创建creep
 * @param {Spawn} spawn 
 * @param {*} taskID 
 */
function spawnCreepByTask(spawn, taskID, taskDeta = null) {
    spawn = objIDToObj(spawn)
    let result = false
    // console.log('taskDeta的存在情况' + JSON.stringify(taskDeta));
    if (!taskDeta) taskDeta = taskManager.getTaskByID(taskID, spawn.id, spawn.pos.roomName, false)['taskDeta']
    let taskParams = taskDeta['taskParams']
    let { body, energExpend } = getCreepBodyByRGL({ room: spawn.room, tag: taskParams['tag'], role: taskParams['role'] })
    if (spawn.room.energyAvailable) {
        if (energExpend && energExpend > 300) {
            if (spawn.room.memory.creeps['worker'][creepRoleEnum.worker.harvester].length <= 2) {
                body = [WORK, CARRY, MOVE, MOVE]
            }
        }
        // 满足条件，则领取任务，进入生产流程
        let creepName = taskParams.role + Game.time
        let resultCode = spawn.spawnCreep(body, creepName, {
            memory: {
                tag: taskParams.tag,
                role: taskParams.role,
                rule: taskParams.rule,
                targetID: taskParams.targetID,
                targetPos: taskParams.targetPos,
                state: {
                    isWorking: false,
                    isRuning: false,
                    isBlock: false,
                    tick: 0,
                    pos: spawn.pos,
                    path: []
                },
                params: taskParams.params,
            }
        })
        // console.log(`spawnCreep的逻辑结果编码${resultCode}`);
        switch (resultCode) {
            case OK:
                result = true
                taskManager.getTaskByID(taskID, spawn.id, spawn.pos.roomName)
                // 满足生产，则在生产完毕之后唤醒
                eventManager.add(`${spawn.name}开始生产creep`, 1, eventMap.spawnCreep, { spawnID: spawn.id, taskID: taskID, targetID: taskParams.targetID[0] })
                Memory.rooms[spawn.pos.roomName]['structure'][spawn.structureType][spawn.id].isRuning = true
                console.log(`${spawn.pos.roomName}的${spawn.name}生产了${creepName},身体部件为${body.join()}`);
                break;
            case ERR_NOT_ENOUGH_ENERGY:
                break;

            default:
                console.log(`使用spawnCreep的逻辑可能出现问题了,错误编码${resultCode}`);
                break;
        }
        return result
    }
}
/**根据条件判断是否允许生产creep */
function isAllowCreateCreep(room, body) {
    room = roomNameToObj(room)

}
/**根据能量等级获取对应的creep身体部件
 * @returns 返回{ body: 部件数组, energExpend: 消耗能量 }
 */
function getCreepBodyByRGL({ room, tag = 'worker', role = 'worker', }) {
    room = roomNameToObj(room)
    let bodymodul = [WORK, CARRY, MOVE, MOVE]
    if (tag == 'worker') {
        // 工作者
        return getCreepBodyExpendByTemp(room, creepBodyEnum[role])
        // TODO：根据工作任务进行特化
        // switch (role) {
        //     case creepRoleEnum.worker.worker:
        //         break;
        //     case creepRoleEnum.worker.upgrader:
        //         break;
        //     case creepRoleEnum.worker.harvester:
        //         break;
        //     default:
        //         break;
        // }
    } else {
        console.log('暂时没有solider模块');
        return null
    }
}

/**根据房间可用能量进行倍化
 * @param {Room} room 
 * @param {Array} body 
 * @returns 返回{ body: 部件数组, energExpend: 消耗能量 }
 */
function getCreepBodyExpendByTemp(room, body = [WORK, CARRY, MOVE, MOVE]) {
    room = roomNameToObj(room)
    // 通过计算获取当前类型模板使用的能量总数
    let parts = {}
    let energExpend = 0
    body.forEach(part => !parts[part] ? parts[part] = 1 : parts[part] += 1);
    Object.keys(parts).forEach(partName => {
        switch (partName) {
            case WORK:
                energExpend += 100 * parts[partName]
                break;
            case CARRY:
            case MOVE:
                energExpend += 50 * parts[partName]
                break;
            case ATTACK:
                energExpend += 80 * parts[partName]
                break;
            case RANGED_ATTACK:
                energExpend += 150 * parts[partName]
                break;
            case HEAL:
                energExpend += 250 * parts[partName]
                break;
            case CLAIM:
                energExpend += 600 * parts[partName]
                break;
            case TOUGH:
                energExpend += 10 * parts[partName]
                break;
            default:
                break;
        }
    })
    // 可用倍数
    const scale = Math.floor(room.energyCapacityAvailable / energExpend)
    if (scale >= 1) {
        for (let count = 1; count < scale; count++) {
            body = body.concat(body)
        }
        energExpend = energExpend * scale
    } else {
        body = [WORK, CARRY, MOVE, MOVE]
        energExpend = 300
    }
    return { body: body, energExpend: energExpend }
}


export {
    spawnCreepByTask,
    isAllowCreateCreep,
    getCreepBodyByRGL
}